// packageB/pages/package/liangdeng/bind_positon/bind_positon.js
const mybehavior = require("../../../../../behaviors/requests")
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/order_system",
    company: "",
    manager: "",
    line: "",
    position: "",
    position_list: [],
    position_list1: [],
    label_mac: "",
    label_list: [],
    indicator_mac: "",
    indicator_list: [],
    options: [],
    options1: [],
    options11: [],
    search_value: "",
    options22: [],
    search_value2: "",
    options3: [],
    options4: [],
    options5: [],
    options6: [],
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    popup_show4: false,
    popup_show5: false,
    popup_show6: false,
    list: [],
    checked: false,
    select_list: [],
    select_btn: true,
    delete_show: false,
    delete_radio: [],
    delete_flag: "",
    delete_data: {},
    page_size: 5
  },
  // 绑定
  updateData(e) {
    wx.navigateTo({
      url: '/packageB/pages/package/liangdeng/update_bind/update_bind',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.row_data)

      }
    })
  },
  // 选择对象
  onConfirmDelete() {
    let key = this.data.delete_radio
    let flag = this.data.delete_flag
    console.log(key);
    console.log(flag);
    if (key[0] == "1" && key.length == 1 && flag == "one_delete") {
      console.log("单标签");

      let row = this.data.delete_data
      let url = this.data.baseUrl + "/delete_warehouse_position"
      let data = {
        company: row.company,
        manager: row.manager,
        key: row.key,
        label: 1,
        indicator: 0,
      }
      this.post(url, data).then(res => {
        console.log("删除绑定关系");
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.getDatas()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    } else if (key[0] == "1" && key.length == 1 && flag == "many_delete") {
      console.log("多标签");

      let list1 = JSON.parse(JSON.stringify(this.data.select_list))
      for (let i in list1) {
        let url = this.data.baseUrl + "/delete_warehouse_position"
        let data = {
          company: list1[i].company,
          manager: list1[i].manager,
          key: list1[i].key,
          label: 1,
          indicator: 0,
        }
        this.post(url, data).then(res => {
          console.log("删除绑定关系");
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
              icon: 'success'
            })
            this.getDatas()
            this.setData({
              select_btn: true

            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        }).catch(err => {
          console.log(err);
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      }

      this.setData({
        select_list: []

      })

    } else if (key[0] == "2" && key.length == 1 && flag == "one_delete") {
      console.log("删除单个巷道灯");
      let row = this.data.delete_data
      let url = this.data.baseUrl + "/delete_warehouse_position"
      let data = {
        company: row.company,
        manager: row.manager,
        key: row.key,
        label: 0,
        indicator: 1,
      }
      this.post(url, data).then(res => {
        console.log("删除绑定关系");
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.getDatas()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    } else if (key[0] == "2" && key.length == 1 && flag == "many_delete") {
      console.log("多巷道");

      let list1 = JSON.parse(JSON.stringify(this.data.select_list))
      for (let i in list1) {
        let url = this.data.baseUrl + "/delete_warehouse_position"
        let data = {
          company: list1[i].company,
          manager: list1[i].manager,
          key: list1[i].key,
          label: 0,
          indicator: 1,
        }
        this.post(url, data).then(res => {
          console.log("删除绑定关系");
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
              icon: 'success'
            })
            this.getDatas()
            this.setData({
              select_btn: true

            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        }).catch(err => {
          console.log(err);
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      }
      this.setData({
        select_list: []

      })

    } else if (key.length == 2 && flag == "one_delete") {
      console.log("一条数据全删");
      let row = this.data.delete_data
      let url = this.data.baseUrl + "/delete_warehouse_position"
      let data = {
        company: row.company,
        manager: row.manager,
        key: row.key,
        label: 1,
        indicator: 1,
      }
      this.post(url, data).then(res => {
        console.log("删除绑定关系");
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.getDatas()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    }
    else if (key.length == 2 && flag == "many_delete") {
      console.log("多条数据全删");
      let list1 = JSON.parse(JSON.stringify(this.data.select_list))
      for (let i in list1) {
        let url = this.data.baseUrl + "/delete_warehouse_position"
        let data = {
          company: list1[i].company,
          manager: list1[i].manager,
          key: list1[i].key,
          label: 1,
          indicator: 1,
        }
        this.post(url, data).then(res => {
          console.log("删除绑定关系");
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
              icon: 'success'
            })
            this.getDatas()
            this.setData({
              select_btn: true

            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        }).catch(err => {
          console.log(err);
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      }
      this.setData({
        select_list: []

      })

    }
    else {
      wx.showToast({
        title: '请选择！！',
        icon: 'error'
      })
    }
  },
  onChangeDelete(event) {
    console.log(event.detail);
    this.setData({
      delete_radio: event.detail,
    });
  },
  onCloseDelete() {
    this.setData({
      delete_radio: "",
      delete_show: false,
      delete_flag: "",
    });
  },
  // 删除数据
  deleteData(e) {
    console.log(e.currentTarget.dataset);
    let flag = e.currentTarget.dataset.delete_flag
    if (flag == "one_delete") {
      let row = e.currentTarget.dataset.row_data
      this.setData({
        delete_show: true,
        delete_flag: flag,
        delete_data: row
      })

    } else {
      if (this.data.select_list.length != 0) {
        this.setData({
          delete_show: true,
          delete_flag: flag,
        })
      } else {
        wx.showToast({
          title: '请勾选数据！',
          icon: 'error'
        })
      }

    }


  },
  // 选择数据
  onChangeCheckbox(e) {
    console.log(e.currentTarget.dataset.row_data);
    let row = e.currentTarget.dataset.row_data
    let list1 = this.data.list
    let list2 = JSON.parse(JSON.stringify(this.data.select_list))
    list1[row.index - 1].checked = !list1[row.index - 1].checked

    if (list1[row.index - 1].checked == true) {
      list2.push(row)
    } else {
      list2.splice(row.index - 1, 1)
    }
    this.setData({
      list: list1,
      select_list: list2
    })
    console.log("选中的值", this.data.select_list);



  },
  // 全选
  allSelectBtn() {
    let list1 = this.data.list
    for (let i in list1) {
      list1[i].checked = true
    }
    this.setData({
      list: list1,
      select_list: list1,
      select_btn: false

    })
  },
  // 全不选
  allNoSelectBtn() {
    let list1 = this.data.list
    for (let i in list1) {
      list1[i].checked = false
    }
    this.setData({
      list: list1,
      select_list: [],
      select_btn: true

    })
  },
  // 批量删除
  manyDelect() {
    Dialog.confirm({
      title: '确定删除选中数据？',
    })
      .then(() => {
        let list1 = JSON.parse(JSON.stringify(this.data.select_list))
        for (let i in list1) {
          let url = this.data.baseUrl + "/delete_warehouse_position"
          let data = {
            company: list1[i].company,
            manager: list1[i].manager,
            key: list1[i].key,
          }
          this.post(url, data).then(res => {
            console.log("删除绑定关系");
            if (res.code == 20000) {
              wx.showToast({
                title: res.msg,
                icon: 'success'
              })
              this.getDatas()
              this.setData({
                select_btn: true

              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          }).catch(err => {
            console.log(err);
            wx.showToast({
              title: err.msg,
              icon: 'error'
            })
          })
        }
      })
      .catch(() => {
        // on cancel
      });



  },
  // 删除
  delete(e) {
    Dialog.confirm({
      title: '是否确认删除',
    })
      .then(() => {
        console.log(e.currentTarget.dataset.row_data);
        let row = e.currentTarget.dataset.row_data
        let url = this.data.baseUrl + "/delete_warehouse_position"
        let data = {
          company: row.company,
          manager: row.manager,
          key: row.key,
        }
        this.post(url, data).then(res => {
          console.log("删除绑定关系");
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
              icon: 'success'
            })
            this.getDatas()
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
        }).catch(err => {
          console.log(err);
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  // 去修改
  toUpdate(e) {
    console.log(e.currentTarget.dataset.row_data);
    let row = e.currentTarget.dataset.row_data
  },
  onSearch1(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options1: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options1: arr
      })
    }
  },
  onSearch2(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options22
      this.setData({
        options2: arr
      })
    } else {
      let list1 = this.data.options22
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value2) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options2: arr
      })
    }
  },
  // 点击--公司名称--选取输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择--公司名称--输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确认--公司名称值--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    const arr = this.data.options[value]
    wx.setStorageSync('lk_company', value)

    this.setData({
      company: value,
      options2: arr,
      options22: arr,
      popup_show1: false,
      manager: "",
      line: "",
    })
  },

  // 点击--单位--选取输入框
  onClick2(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show2: true
    })
  },
  // 选择--单位--输入框关闭
  onClose2() {
    this.setData({ popup_show2: false });
  },
  // 确认--单位--弹出框关闭
  onChange2(event) {
    const { picker, value, index } = event.detail;
    wx.setStorageSync('lk_manager', value)
    this.setData({
      manager: value,
      line: "",
      popup_show2: false,
    })
    this.getLineDatas()
  },


  // 获取--公司名称--数据
  getCompanyDatas() {
    let url = this.data.baseUrl + "/get_u_permission"
    let data = {

    }
    this.post(url, data).then(res => {
      // console.log("公司名称", res.data)
      let data_list = Object.keys(res.data)

      if (res.code == 20000) {

        this.setData({
          options1: data_list,
          options11: data_list,
          options: res.data,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },


  // 点击--仓库选取--选取输入框
  onClick3(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show3: true
    })
  },
  // 选择--仓库选取--输入框关闭
  onClose3() {
    this.setData({ popup_show3: false });
  },
  // 确认--仓库选取--弹出框关闭
  onChange3(event) {
    const { picker, value, index } = event.detail;
    wx.setStorageSync('lk_line', value)
    this.setData({
      line: value,
      popup_show3: false,
    })
    this.getDatas()
  },




  // 获取--仓库选取--数据
  getLineDatas() {
    let url = this.data.baseUrl + "/positions_lines"
    let data = {
      company: this.data.company,
      manager: this.data.manager,
    }
    this.post(url, data).then(res => {
      console.log("仓库选取", res.data)

      if (res.code == 20000) {

        this.setData({
          options3: res.data,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  // 获取--绑定数据--数据
  getDatas() {
    wx.showLoading({
      title: '获取数据中',
    })
    let url = this.data.baseUrl + "/query_warehouse_positions"
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      position: this.data.position,
      label_mac: this.data.label_mac,
      indicator_mac: this.data.indicator_mac,
      page: 1,
      page_size: this.data.page_size
    }
    this.post(url, data).then(res => {
      console.log("绑定数据", res.data)
      for (let i in res.data.results) {
        res.data.results[i].checked = false
      }

      if (res.code == 20000) {

        this.setData({
          list: res.data.results
        })
        wx.hideLoading()

      } else {
        this.setData({
          list: []
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        list: []
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 扫描
  scan1(e) {
    console.log(e.currentTarget.dataset.row_data);
    let key = e.currentTarget.dataset.row_data
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code
        })
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },


  // 点击--库位号码--选取输入框
  onClick4(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show4: true
    })
  },
  // 选择--库位号码--输入框关闭
  onClose4() {
    this.setData({ popup_show4: false });
  },
  // 确认--库位号码--弹出框关闭
  onChange4(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      position: value,
      popup_show4: false,
    })
    this.getDatas()
  },

  // 点击--标签号码--选取输入框
  onClick5(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show5: true
    })
  },
  // 选择--标签号码--输入框关闭
  onClose5() {
    this.setData({ popup_show5: false });
  },
  // 确认--标签号码--弹出框关闭
  onChange5(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      label_mac: value,
      popup_show5: false,
    })
    this.getDatas()
  },

  // 点击--巷道灯码--选取输入框
  onClick4(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show6: true
    })
  },
  // 选择--巷道灯码--输入框关闭
  onClose6() {
    this.setData({ popup_show6: false });
  },
  // 确认--巷道灯码--弹出框关闭
  onChange6(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      indicator_mac: value,
      popup_show6: false,
    })
    this.getDatas()
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCompanyDatas()
    // this.getDatas()
    this.setData({
      company: wx.getStorageSync('lk_company'),
      manager: wx.getStorageSync('lk_manager'),
      line: wx.getStorageSync('lk_line'),
    })
    this.getLineDatas()
    this.getDatas()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showLoading({
      title: '获取数据中',
    })
    this.setData({
      page_size: this.data.page_size + 5
    })

    this.getDatas()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})